package cn.schoolwow.quickdao.module.operation.flow.upgrade;

import cn.schoolwow.quickdao.domain.DAO;
import cn.schoolwow.quickdao.domain.QuickDAOConfig;
import cn.schoolwow.quickdao.domain.database.parent.UpgradeRequest;
import cn.schoolwow.quickdao.domain.entity.DatabaseUpgrade;
import cn.schoolwow.quickdao.domain.entity.Entity;
import cn.schoolwow.quickdao.module.database.dml.common.domain.ManipulationOption;
import cn.schoolwow.quickdao.module.database.dml.instance.flow.insert.common.SetInsertInstanceParameterFlow;
import cn.schoolwow.quickdao.module.database.dml.instance.flow.insert.common.SetInsertInstanceStatementFlow;
import cn.schoolwow.quickdao.module.database.dql.query.service.FetchListFlow;
import cn.schoolwow.quickdao.module.database.parent.flow.executor.ExecuteUpdateConnectionFlow;
import cn.schoolwow.quickdao.module.database.parent.flow.executor.common.UpdateLogFlow;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import com.alibaba.fastjson.JSONArray;

public class DatabaseUpgradeFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        UpgradeRequest upgradeRequest = flowContext.checkInstanceData(UpgradeRequest.class);
        QuickDAOConfig quickDAOConfig = flowContext.checkInstanceData(QuickDAOConfig.class);

        DAO dao = quickDAOConfig.dao;

        Entity entity = dao.getEntity(DatabaseUpgrade.class);
        if(null==entity){
            throw new IllegalArgumentException("日志类信息获取失败!日志类名:"+DatabaseUpgrade.class.getName());
        }
        flowContext.startFlow(new FetchListFlow())
                .putTemporaryData("entity", entity)
                .putTemporaryData("column", "comment")
                .putTemporaryData("value", upgradeRequest.comment)
                .execute();
        JSONArray resultSetArray = (JSONArray) flowContext.getData("resultSetArray");
        DatabaseUpgrade databaseUpgrade = resultSetArray.isEmpty()?null:resultSetArray.toJavaList(DatabaseUpgrade.class).get(0);
        if(null!=databaseUpgrade){
            if(!databaseUpgrade.isSuccess()){
                upgradeRequest.upgradeConsumer.accept(dao);
                databaseUpgrade.setSuccess(true);
                flowContext.startFlow(new UpdateLogFlow())
                        .putTemporaryData("instance", databaseUpgrade)
                        .execute();
            }
            return;
        }
        databaseUpgrade = new DatabaseUpgrade();
        databaseUpgrade.setComment(upgradeRequest.comment);
        dao.startRecord();
        try {
            upgradeRequest.upgradeConsumer.accept(dao);
            databaseUpgrade.setSuccess(true);
        }catch (Exception e){
            databaseUpgrade.setSuccess(false);
            throw e;
        }finally {
            String sql = dao.stopRecord();
            databaseUpgrade.setSql(sql);

            ManipulationOption manipulationOption = new ManipulationOption();
            manipulationOption.returnGeneratedKeys = false;
            flowContext.startFlow("插入数据库升级语句")
                    .putTemporaryData("insertSqlLog", false)
                    .putTemporaryData("name", "插入数据库升级语句")
                    .putInstanceData(manipulationOption)
                    .putTemporaryData("entity", entity)
                    .putTemporaryData("instance", databaseUpgrade)
                    .next(new SetInsertInstanceStatementFlow())
                    .next(new SetInsertInstanceParameterFlow())
                    .next(new ExecuteUpdateConnectionFlow())
                    .execute();
        }
    }

    @Override
    public String name() {
        return "数据库升级";
    }
}
